<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/28
 * Time: 08:36
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use fast\Tree;
use think\Cache;

/**
 * 冻结功能节点管理
 * Class FunctionNode
 * @package app\admin\controller
 */
class FunctionNode extends Backend
{
    protected $model = null;
    protected $node_list = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('FunctionNode');
        // 必须将结果集转换为数组
        $node_list = collection($this->model->order('id', 'desc')->select())->toArray();
//        foreach ($node_list as $k => &$v)
//        {
//            $v['title'] = __($v['title']);
//            $v['remark'] = __($v['remark']);
//        }
        unset($v);
        $tree =  Tree::instance()->init($node_list);
        $tree->pidname = 'parent_id';
        $this->node_list = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $node_data = [0 => __('None')];
        foreach ($this->node_list as $k => &$v)
        {
            if ($v['parent_id'] != 0)
                continue;
            $node_data[$v['id']] = $v['name'];
        }
        $this->view->assign('node_list', $node_data);
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            $list = $this->node_list;
            $total = count($this->node_list);

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params)
            {
//                if (!$params['ismenu'] && !$params['pid'])
//                {
//                    $this->error(__('The non-menu rule must have parent'));
//                }
                $this->model->create($params);
//                Cache::rm('__menu__');
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params)
            {
//                if (!$params['ismenu'] && !$params['pid'])
//                {
//                    $this->error(__('The non-menu rule must have parent'));
//                }
                $row->save($params);
//                Cache::rm('__menu__');
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $delIds = [];
            foreach (explode(',', $ids) as $k => $v)
            {
                $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
            }
            $delIds = array_unique($delIds);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count)
            {
                Cache::rm('__menu__');
                $this->success();
            }
        }
        $this->error();
    }

}